﻿namespace Shanda.DevFx.Security.Web.Pages
{
    using Shanda.DevFx.Core;
    using Shanda.DevFx.Data.Attributes;
    using Shanda.DevFx.Data.Utils;
    using Shanda.DevFx.Security;
    using Shanda.DevFx.Security.Web;
    using Shanda.DevFx.Utils;
    using System;
    using System.Web.UI.WebControls;

    public class UserEditPage : Shanda.DevFx.Security.Web.BasePage
    {
        protected LinkButton btnAddGroups;
        protected LinkButton btnAddRoles;
        protected LinkButton btnRemoveGroups;
        protected LinkButton btnRemoveRoles;
        protected Button btnSubmit;
        [Column("IsAvailable", ColumnType=typeof(bool))]
        protected DropDownList ddlIsAvailable;
        [Column("IsInnerUser", ColumnType=typeof(bool))]
        protected DropDownList ddlIsInnerUser;
        protected Label lblMessage;
        protected ListBox lbxAailRoles;
        protected ListBox lbxAvailGroups;
        protected ListBox lbxGroups;
        protected ListBox lbxRoles;
        protected bool newItem;
        protected RequiredFieldValidator rfvUserName;
        protected RequiredFieldValidator rfvUserNo;
        protected TextBox txtPassword;
        [Column("UserName")]
        protected TextBox txtUserName;
        protected TextBox txtUserNo;
        protected string userNo;
        protected ValidationSummary vsUser;

        protected virtual void btnAddGroups_Click(object sender, EventArgs e)
        {
            string str = null;
            for (int i = 0; i < this.lbxAvailGroups.get_Items().get_Count(); i++)
            {
                ListItem item = this.lbxAvailGroups.get_Items().get_Item(i);
                if (item.get_Selected() && !this.lbxGroups.get_Items().Contains(item))
                {
                    if (!this.newItem)
                    {
                        IAOPResult result = Securitor.GroupManager.AddUserToGroup(this.userNo, item.get_Value());
                        if (result.IsFailed)
                        {
                            str = str + "<br>添加用户到组失败：" + result.ResultDescription;
                            i++;
                            goto Label_0086;
                        }
                    }
                    this.lbxGroups.get_Items().Add(item);
                Label_0086:;
                }
            }
            this.lblMessage.set_Text(str);
        }

        protected virtual void btnAddRoles_Click(object sender, EventArgs e)
        {
            string str = null;
            for (int i = 0; i < this.lbxAailRoles.get_Items().get_Count(); i++)
            {
                ListItem item = this.lbxAailRoles.get_Items().get_Item(i);
                if (item.get_Selected() && !this.lbxRoles.get_Items().Contains(item))
                {
                    if (!this.newItem)
                    {
                        IAOPResult result = Securitor.UserManager.AddUserToRole(this.userNo, item.get_Value());
                        if (result.IsFailed)
                        {
                            str = str + "<br>添加用户到角色失败：" + result.ResultDescription;
                            i++;
                            goto Label_0086;
                        }
                    }
                    this.lbxRoles.get_Items().Add(item);
                Label_0086:;
                }
            }
            this.lblMessage.set_Text(str);
        }

        protected virtual void btnRemoveGroups_Click(object sender, EventArgs e)
        {
            string str = null;
            int num = 0;
            while (num < this.lbxGroups.get_Items().get_Count())
            {
                ListItem item = this.lbxGroups.get_Items().get_Item(num);
                if (item.get_Selected())
                {
                    if (!this.newItem)
                    {
                        IAOPResult result = Securitor.GroupManager.RemoveUserFromGroup(this.userNo, item.get_Value());
                        if (result.IsFailed)
                        {
                            str = str + "<br>组中移除用户失败：" + result.ResultDescription;
                            num++;
                            continue;
                        }
                    }
                    this.lbxGroups.get_Items().Remove(item);
                }
                else
                {
                    num++;
                }
            }
            this.lblMessage.set_Text(str);
        }

        protected virtual void btnRemoveRoles_Click(object sender, EventArgs e)
        {
            string str = null;
            int num = 0;
            while (num < this.lbxRoles.get_Items().get_Count())
            {
                ListItem item = this.lbxRoles.get_Items().get_Item(num);
                if (item.get_Selected())
                {
                    if (!this.newItem)
                    {
                        IAOPResult result = Securitor.UserManager.RemoveUserFromRole(this.userNo, item.get_Value());
                        if (result.IsFailed)
                        {
                            str = str + "<br>角色中移除用户失败：" + result.ResultDescription;
                            num++;
                            continue;
                        }
                    }
                    this.lbxRoles.get_Items().Remove(item);
                }
                else
                {
                    num++;
                }
            }
            this.lblMessage.set_Text(str);
        }

        protected virtual void btnSubmit_Click(object sender, EventArgs e)
        {
            if (!base.get_IsValid())
            {
                this.lblMessage.set_Text("请正确填写表单");
            }
            else
            {
                User u = this.LoadUser();
                this.FillUser(u);
                IAOPResult result = this.SaveUser(u);
                string resultDescription = "成功保存";
                if (result.IsFailed)
                {
                    resultDescription = result.ResultDescription;
                }
                else if (this.newItem)
                {
                    foreach (ListItem item in this.lbxRoles.get_Items())
                    {
                        result = Securitor.UserManager.AddUserToRole(u.UserNo, item.get_Value());
                        if (result.IsFailed)
                        {
                            resultDescription = resultDescription + "<br>用户添加成功，但用户添加到角色失败：" + result.ResultDescription;
                        }
                    }
                    foreach (ListItem item2 in this.lbxGroups.get_Items())
                    {
                        result = Securitor.GroupManager.AddUserToGroup(u.UserNo, item2.get_Value());
                        if (result.IsFailed)
                        {
                            resultDescription = resultDescription + "<br>用户添加成功，但添加用户到组失败：" + result.ResultDescription;
                        }
                    }
                }
                this.lblMessage.set_Text(resultDescription);
            }
        }

        protected virtual void FillForm(User u)
        {
            this.txtUserNo.set_Text(u.UserNo);
            this.txtUserNo.set_ReadOnly(true);
            DataTransfer.TransEntityPropertyToWebFormData(u, this);
            string[] userRoles = Securitor.UserManager.GetUserRoles(this.userNo);
            this.lbxRoles.set_DataTextField("Title");
            this.lbxRoles.set_DataValueField("RoleNo");
            this.lbxRoles.set_DataSource(Securitor.RoleManager.GetItem(userRoles));
            this.lbxRoles.DataBind();
            this.lbxGroups.set_DataTextField("IndentTitle");
            this.lbxGroups.set_DataValueField("GroupNo");
            this.lbxGroups.set_DataSource(Securitor.GroupManager.GetUserGroups(this.userNo));
            this.lbxGroups.DataBind();
        }

        protected virtual void FillUser(User u)
        {
            DataTransfer.TransWebFormDataToEntityProperty(this, u);
        }

        protected override void InitData()
        {
            base.InitData();
            this.userNo = base.get_Request().get_Params()["UserNo"];
            this.newItem = Checker.CheckEmptyString("UserNo", this.userNo, false);
            if (!base.get_IsPostBack())
            {
                this.lbxAailRoles.set_DataTextField("Title");
                this.lbxAailRoles.set_DataValueField("RoleNo");
                this.lbxAailRoles.set_DataSource(Securitor.RoleManager.ListItem());
                this.lbxAailRoles.DataBind();
                this.lbxAvailGroups.set_DataTextField("IndentTitle");
                this.lbxAvailGroups.set_DataValueField("GroupNo");
                this.lbxAvailGroups.set_DataSource(Securitor.GroupManager.ListItem());
                this.lbxAvailGroups.DataBind();
                if (!this.newItem)
                {
                    User item = Securitor.UserManager.GetItem(this.userNo);
                    if (item == null)
                    {
                        WebHelper.ShowMessage("没找到此用户，或者您没有权限操作", "history.back()", true, true);
                    }
                    this.FillForm(item);
                }
            }
        }

        private void InitializeComponent()
        {
            this.btnRemoveGroups.add_Click(new EventHandler(this.btnRemoveGroups_Click));
            this.btnAddGroups.add_Click(new EventHandler(this.btnAddGroups_Click));
            this.btnRemoveRoles.add_Click(new EventHandler(this.btnRemoveRoles_Click));
            this.btnAddRoles.add_Click(new EventHandler(this.btnAddRoles_Click));
            this.btnSubmit.add_Click(new EventHandler(this.btnSubmit_Click));
        }

        protected virtual User LoadUser()
        {
            User item;
            if (this.newItem)
            {
                item = Securitor.UserManager.CreateItem();
                item.UserNo = this.txtUserNo.get_Text();
                item.Password = this.txtPassword.get_Text();
                return item;
            }
            item = Securitor.UserManager.GetItem(this.userNo);
            if (!Checker.CheckEmptyString("Password", this.txtPassword.get_Text(), false))
            {
                item.Password = this.txtPassword.get_Text();
            }
            return item;
        }

        protected override void OnInit(EventArgs e)
        {
            this.InitializeComponent();
            base.OnInit(e);
        }

        protected virtual IAOPResult SaveUser(User u)
        {
            return u.Save();
        }
    }
}

